package app.com.freshmanapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONObject;

import app.com.freshmanapp.pojo.User;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText et_name,et_password;
Button bt_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        bt_login.setOnClickListener(this);
    }

    private void init() {
        et_name=(EditText) findViewById(R.id.name);
        et_password=(EditText) findViewById(R.id.password);
        bt_login=(Button) findViewById(R.id.btn);
    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String password=et_password.getText().toString();
        User u=new User(name,password);
        Gson gson=new Gson();
        String json = gson.toJson(u);
        System.out.println(json);
        //网络请求
        String url="http://10.2.64.50:8080/app2/user/regist";
        //请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        //请求对象
        Request request=new JsonObjectRequest(Request.Method.POST, url, json, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    boolean success = jsonObject.getBoolean("success");
                    if (success){
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }


                //成功响应

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //响应失败
                System.out.println("请求失败");

            }
        });
        //将请求对象放到请求队列里
        requestQueue.add(request);


    }
}